import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import {formatMessage} from './intl';
import stringify from 'Shared/utils/stringify';

const baseUrl = '/afterSales/api/v1';

const getListDesc = formatMessage({
    id: 'api.getList',
    defaultMessage: '查询GoodWill预算数据'
});

const getDetailDesc = formatMessage({
    id: 'api.getDetail',
    defaultMessage: '查询GoodWill预算数据详情'
});

const getAddDesc = formatMessage({
    id: 'api.getAdd',
    defaultMessage: '新增GoodWill预算数据'
});

const getModifyDesc = formatMessage({
    id: 'api.getModify',
    defaultMessage: '修改GoodWill预算数据'
});

const getAbandonDesc = formatMessage({
    id: 'api.getAbandon',
    defaultMessage: '作废'
});

const getDealersDesc = formatMessage({
    id: 'api.message.getDealersDesc',
    defaultMessage: '查询总代信息'
});
// 初始化查询
export const initQuery = () => request(`${baseUrl}/ui/goodWillBudget/init`, null, null)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery));

// 查询GoodWill预算数据列表
export const getList = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/goodWillBudgets?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};

// 查询GoodWill预算数据详情
export const getDetail = id => request(`${baseUrl}/goodWillBudgets/${id}`, null, null)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailDesc));

// 新增
export const newOrder = data => request(`${baseUrl}/goodWillBudgets`, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getAddDesc));

// 修改
export const modifyOrder = (id, data) => request(`${baseUrl}/goodWillBudgets/${id}`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getModifyDesc));

// 作废
export const abandonOrder = (id, rowVersion) => request(`${baseUrl}/goodWillBudgets/${id}/abandon`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify({
        rowVersion
    })
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getAbandonDesc));
// 快捷查询总代
export const getDealersQuickly = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/dealers/forGoodWillBudget/simple?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDealersDesc));
};
// 查询总代
export const getDealers = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/dealers/forGoodWillBudget?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDealersDesc));
};

